import { getTree } from '@/services/Organization/viewsService';
// import a from '../Orgs/demo.json';

export const addCEO = result => {
    // TODO 临时修改康总节点，并增加康总助理
    const tmpCompany = result.data.company;
    // const ceo = tmpCompany[0];
    // 集团时才做
    // if (result.data.id === 100) {
    const findIndex = result.data.company[0].employeeList.findIndex(item => item.id === 198);
    if (findIndex !== -1) {
        const dsz = result.data.company[0].employeeList[findIndex];
        tmpCompany[0] = {
            ...tmpCompany[0],
            managerName: dsz.employeeName,
            managerPostName: dsz.postName,
            managerId: dsz.id
        };
        // 删掉CEO，因为他有单独节点
        tmpCompany[0].employeeList.splice(findIndex, 1);
        // 过滤掉李佩珊，因为他是CEO助理
        tmpCompany[0].employeeList = tmpCompany[0].employeeList.filter(item => item.id !== 1066);
        // tmpCompany[0].subChildren = [
        //     {
        //         ...ceo,
        //         isCEO: true,
        //         subChildren: [],
        //         children: tmpCompany[0].subChildren || [],
        //         employeeList: [
        //             {
        //                 id: 1066,
        //                 employeeName: '李佩珊',
        //                 userCode: '',
        //                 postName: 'CEO助理',
        //                 employeeRank: '',
        //                 inServiceStatus: '正式'
        //             }
        //         ]
        //     }
        // ];
    } else {
        console.error('没董事长不行！');
    }
    // }

    return tmpCompany;
};

export default {
    namespace: 'orgsView',
    state: {
        companyTree: {
            company: []
        }
    },
    effects: {
        *fetchCompanyTree({ payload }, { put, call }) {
            const result = yield call(getTree, payload.oid);

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        companyTree: {
                            ...result.data,
                            company: addCEO(result)
                        }
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clearCompanyTree(state) {
            return {
                ...state,
                companyTree: {
                    company: []
                }
            };
        }
    }
};
